package com.ardf.controller;

import com.ardf.entity.dto.SignInTableDto;
import com.ardf.entity.vo.SignInUpdateOneVo;
import com.ardf.service.ISigninService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/signin")
public class SigninController {
    private final ISigninService signinService;
    public SigninController(ISigninService signinService){
        this.signinService=signinService;
    }
    /**
     * 搜索查询
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<SignInTableDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                          @RequestParam("pageNum") Integer pageNum,
                                                          @RequestParam("competitionId") Long competitionId,
                                                          @RequestParam(value = "searchAll", required = false) String searchAll) {
        Page<SignInTableDto> page;
        if (searchAll!=null&&StringUtils.hasLength(searchAll.trim())) {
            page = signinService.searchAll(pageSize, pageNum, competitionId, searchAll.trim());
        } else {
            page = signinService.searchAll(pageSize, pageNum, competitionId, null);
        }
        return ResponseEntity.ok(page);
    }
    /**
     * 批量签到
     */
    @PostMapping("/updateBatch")
    public void updateBatch(@RequestBody List<Long> ids) {
        if(ids!=null&&!ids.isEmpty()){
            signinService.updateBatch(ids);
        }
    }

    /**
     * 单个签到
     */
    @PostMapping("/updateOne")
    public void updateOne(@RequestBody SignInUpdateOneVo signInUpdateOneVo) {
        signinService.updateOne(signInUpdateOneVo);
    }
    /**
     * 下载签到表
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam("competitionId") Long competitionId) throws IOException {
        signinService.download(response,competitionId);
    }
}
